<template>
  <div class="container">
    <div class="sbgl-nav breadcrumb">
      <img src="static/img/shu.png" alt="">
      <span>资源管理</span>
      <span class="glyphicon glyphicon-chevron-right"></span>
      <span>平台管理</span>
      <span class="glyphicon glyphicon-chevron-right"></span>
      <span>{{ isAdd ? '添加下级平台' : '修改下级平台' }}</span>
    </div>
    <form class="form-horizontal" v-if="isAdd" @submit.prevent="submit">
      <div class="form-group">
          <label for="inputnumber" class="col-sm-2 control-label col-sm-offset-3">下级平台IP：</label>
          <div class="col-sm-4">
            <input type="text" class="form-control" id="inputtext" name="remoteip" value="" v-model="remoteip">
          </div>
      </div>
      <div class="form-group">
          <label for="inputnumber2" class="col-sm-2 control-label col-sm-offset-3">下级平台端口：</label>
          <div class="col-sm-4">
            <input type="number" class="form-control" id="inputnumber2" placeholder="" name="port" value="" v-model="port">
          </div>
      </div>
      <p class="lx_btn">
        <button type="submit" class="btn btn-default btn-lg">保存</button>
        <button type="button" onclick="window.history.go(-1)" class="btn btn-default btn-lg">返回</button>
      </p>
    </form>

    <form class="form-horizontal" v-else @submit.prevent="submit">
      <div class="form-group">
          <label for="inputtext" class="col-sm-2 control-label col-sm-offset-3">下级平台名称：</label>
          <div class="col-sm-4">
            <input type="text" class="form-control" id="inputtext" readonly="readonly" disabled v-bind:value="editPlat.platformname">
          </div>
      </div>
      <div class="form-group">
          <label for="inputtext1" class="col-sm-2 control-label col-sm-offset-3">下级平台ID：</label>
          <div class="col-sm-4">
            <input type="text" class="form-control" id="inputtext1" readonly="readonly" disabled v-bind:value="editPlat.platid">
          </div>
      </div>
      <div class="form-group">
          <label for="inputnumber" class="col-sm-2 control-label col-sm-offset-3">下级平台IP：</label>
          <div class="col-sm-4">
            <input type="text" class="form-control" id="inputtext" name="ip" v-model="editPlat.ip">
          </div>
      </div>
      <div class="form-group">
          <label for="inputnumber2" class="col-sm-2 control-label col-sm-offset-3">下级平台端口：</label>
          <div class="col-sm-4">
            <input type="number" class="form-control" id="inputnumber2" placeholder="" name="port" v-model="editPlat.port">
          </div>
      </div>
      <p class="lx_btn">
        <button type="submit" class="btn btn-default btn-lg">保存</button>
        <button type="button" onclick="window.history.go(-1)" class="btn btn-default btn-lg">返回</button>
      </p>
    </form>
  </div>
</template>

<script>
  export default {
    name: 'addPlat',
    data: function () {
      return {
        remoteip: '',
        method: 'add',
        port: '',
        editPlat: {
          method: 'UpdatePlat',
          platformname: '',
          platid: this.$route.params.platId || '',
          ip: '',
          port: ''
        }
      }
    },
    created: function () {
      this.method = this.$route.params.method
      this.isAdd = this.method === 'add'
      if (!this.isAdd) {
        this.editInit()
      }
    },
    methods: {
      submit: function () {
        var data = {
          method: 'AddPlat',
          remoteip: this.remoteip,
          port: this.port
        }
        if (!this.isAdd) {
          data.method = 'UpdatePlat'
          data.platid = this.editPlat.platid
          data.ip = this.editPlat.ip
          data.port = this.editPlat.port
        }
        this.$http.post('/api/hedacmdreq', data).then(response => {
          var body = response.data.HedaACK.Body
          window.duang.msg(true, body.retMsg)
        })
      },
      editInit: function () {
        this.$http.post('/api/hedacmdreq', {method: 'PlatList', plat: this.editPlat.platid}).then(response => {
          var body = response.data.HedaACK.Body
          if (body.platformlist && body.platformlist.length > 0) {
            this.editPlat = window.$.extend(true, this.editPlat, body.platformlist[0])
          }
        })
      }
    }
  }
</script>
